﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XCore.Extensions;
using XCore.Logger;
using XCore.Domain.Dto.Page;
using SaaS.Core.Authorization;
using XCore.Authorization;
using SaaS.Cache.Sites;
using SaaS.Core;

namespace Web.System.Controllers.Sites
{
    /// <summary>
    ///运营平台
    /// </summary>
    [Route("api/site/[controller]/[action]")]
    [ApiController]
    public class OperationController : SystemControllerBase
    {
        private readonly SiteCacheService _siteCacheService;
        public OperationController(SiteCacheService siteCacheService)
        {
            _siteCacheService = siteCacheService;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Site_Operation)]
        public ActionResult List(PagedSearchInput input)
        {
            return PageResult(SDC.Operations.OrderByDescending(c => c.CreationTime), c => new
            {
                c.EndTime,
                c.Id,
                c.ProjectId,
                c.StartTime,
                c.Status,
                c.UserId,
                UserName = _siteCacheService.GetName(c.UserId),
            });
        }
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Permission(PermissionNames.Site_Operation)]
        public ActionResult Info(int id)
        {
            if (!SDC.Operations.Any(c => c.Id == id)) throw new MessageException(CoreSetting.Error_ID);
            return Ok(SDC.Operations.Where(c => c.Id == id).Select(c => new
            {
                c.Id,
                c.ProjectId,
                c.StartTime,
                c.Status,
                c.Type,
                c.UserId
            }).FirstOrDefault());
        }
    }
}